package com.jr.controller;

import com.google.gson.Gson;
import com.jr.Service.IProjectInfoService;
import com.jr.Service.Impl.IProjectInfoServiceImpl;
import com.jr.pojo.ProjectInfo;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/projectInfo/*")
public class ProjectInfoController extends HttpServlet {
    private IProjectInfoService isp = new IProjectInfoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        int index=requestURI.lastIndexOf("/")+1;
        String mName = requestURI.substring(index);

        if(mName.length()==0){
            mName="showdt";
        }
        Class<ProjectInfoController> cla=ProjectInfoController.class;
        Method method=null;
        try{
            method=cla.getMethod(mName,HttpServletRequest.class,HttpServletResponse.class);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public void showdt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String status = req.getParameter("status");
        int proStatus = 0;
        if(status==null){
            proStatus=-1;
        }else{
            proStatus=Integer.parseInt(status);
        }
        PageHelper ph=new PageHelper();
        ph.setDateCount(isp.queryDataCount(proStatus));
        ph.setPageIndex(Integer.parseInt(req.getParameter("indexpage")));
            ArrayList<ProjectInfo> projectInfos = isp.queryByStatus(proStatus,ph);
        ph.setListIndex(projectInfos);
        resp.getWriter().println(new Gson().toJson(ph));
    }
    public void queryByPid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        int pid=Integer.parseInt(req.getParameter("pid"));
        ProjectInfo projectInfo = isp.queryByPid(pid);
        resp.getWriter().println(new Gson().toJson(projectInfo));

    }
    public void updatePro(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int status=Integer.parseInt(req.getParameter("status"));
        int pid=Integer.parseInt(req.getParameter("pid"));
        ProjectInfo pro=new ProjectInfo();
            pro.setPid(pid);
            pro.setStatus(status);
        boolean b = isp.updateProjectInfo(pro);
        resp.getWriter().println(new Gson().toJson(b));
    }
}

